<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="160px"
    >
      <el-form-item label="积分" prop="addPoint">
        <el-input v-model="dataForm.addPoint" placeholder="积分"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
 
<script>
// import Vue from "vue";
import lodashGet from "lodash/get";
// import { wangEditor } from 'wangeditor'
import quillConfig from '@/utils/quillConfig'

export default {
  // components: {
  //   wangEditor
  //   },
  data() {
    return {
      editor: null,
      toolbarConfig: { },
      quillConfig:'',
      mode: 'default', // or 'simple'
      content: '',
      fileList3:[],
      dialogImageUrlss: "",
      dialogVisibless: false,
      url: "",
      imageUrl: "",
      visible: false,
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      imagesList: [],
      dataForm: {
        id: "",
        name: "",
        text: "",
        sfsj: "",
        money:"",
        moneyJia:"",
        phone:"",
        jj:"",
        photo:"",
        bhxm:"",
        gpxz:"",
        mpId:"",
        lId:"",
      },
      sfsjOptions: [
        {
          value: 1,
          label: "上架"
        },
        {
          value: 0,
          label: "下架"
        }
      ],
      urlOptions: [
        {
          value: '',
          label: "无"
        },
        {
          value: '/pages/scenic/details/index',
          label: "详情"
        }
      ],
      sfzsOptions:[
        {
          value: 0,
          label: "否"
        },
        {
          value: 1,
          label: "是"
        }
      ],
      dataRule: {
        name: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        text: [{ required: true, message: "内容不能为空", trigger: "blur" }],
        sfsj: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        jj: [{ required: true, message: "简介不能为空", trigger: "blur" }],
        sfzs: [{ required: true, message: "请选择跳是否展示", trigger: "blur" }],

      }
    };
  },
  methods: {
    onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
    beforeDestroy() {
        const editor = this.editor
        if (editor == null) return
        editor.destroy() // 组件销毁时，及时销毁编辑器
    },
    inputChangezj(data) {
      var min = 1;
      if (Number(this.dataForm.zj.replace(/[^\d]/g, "")) < min) {
        this.dataForm.zj = min;
      } else {
        this.dataForm.zj = this.dataForm.zj.replace(/[^\d]/g, "");
      }
    },
    inputChangedj(data) {
      var min = 1;
      if (Number(this.dataForm.dj.replace(/[^\d]/g, "")) < min) {
        this.dataForm.dj = min;
      } else {
        this.dataForm.dj = this.dataForm.dj.replace(/[^\d]/g, "");
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.dataForm.photo = res.url;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
      }
      return isLt2M;
    },
    handleAvatarSuccesses(res, file, fileList) {
      this.imagesList.push(res.url);
      console.log(res);
    },
    handleRemovess(file, fileList) {
      let fileUrl = lodashGet(file, ["response", "url"], "");
      if (fileUrl) {
        this.imagesList = this.imagesList.filter(item => item !== fileUrl);
      }
      console.log(file, fileList);
      console.log(this.imagesList);
    },
    handlePictureCardPreviewss(file) {
      this.dialogImageUrlss = file.url;
      this.dialogVisibless = true;
    },
    init(id) {
      if(!id) {
        this.content = ''
        this.imageUrl = ''
      }
      this.url = this.$http.adornUrl(
        `app/oss/upload?token=${this.$cookie.get("token")}`
      );
      this.quillConfig = quillConfig
      this.dataForm.id = id || "";
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/yshbms/invitationCode/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.invitationCodeEntity;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`yshbms/invitationCode/update`),
              method: "post",
              data: this.$http.adornParams({
                  id: this.dataForm.id,
                  addPoint: this.dataForm.addPoint,
                })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.imagesList = []
                    this.fileList3 = []
                    this.imageUrl = ''
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          } else {
            this.$http({
              url: this.$http.adornUrl(`sys/ccp/save`),
              method: "post",
              params: this.$http.adornParams({
                id: this.dataForm.id,
                  name: this.dataForm.name,
              })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.imagesList = []
                    this.fileList3 = []
                    this.imageUrl = ''
                    this.$refs.upload.clearFiles();
                    this.visible = false;
                    this.$emit("refreshDataList");
                  }
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          }
        }
      });
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
